﻿#pragma once
#include "CubeTexture.h"
#include "Material.h"
#include "Path.h"
#include "ShaderProgram.h"


class FCubeTexture;

class FCubeMapMaterial final
	: public FMaterial
{
private:

	std::shared_ptr<FCubeTexture> CubeTexture;


public:

	virtual bool Init() override
	{
		if (!FMaterial::Init())
		{
			return false;
		}

		Shader->SetLightingModel(ELightingModel::None);

		CubeTexture = std::make_shared<FCubeTexture>();

		CubeTexture->LoadFromFile(
			{
					FPath::BuildImgPath("SkyCube/right.jpg"),
					FPath::BuildImgPath("SkyCube/left.jpg"),
					FPath::BuildImgPath("SkyCube/top.jpg"),
					FPath::BuildImgPath("SkyCube/bottom.jpg"),
					FPath::BuildImgPath("SkyCube/front.jpg"),
					FPath::BuildImgPath("SkyCube/back.jpg"),

			});
	
		Shader->Use();

		Shader->SetInt("skybox", 0);

		return true;
	}

	virtual void Use() override
	{
		FMaterial::Use();

		CubeTexture->BindTextureUnit(0);
	}

	const std::string& GetFsSrcCode() const override  // NOLINT(modernize-use-nodiscard)
	{
		static std::string CodeStr = R"(
			#version 410 core
			out vec4 FragColor;
			
			in vec3 TexCoords;
			
			uniform samplerCube skybox;
			
			void main()
			{    
			    FragColor = texture(skybox, TexCoords);
			}
		)";

		return CodeStr;
	}

	const std::string& GetVsSrcCode() const override  // NOLINT(modernize-use-nodiscard)
	{
		static std::string CodeStr = R"(

			#version 410 core 
			
			layout (location = 0) in vec3 aPos;

			out vec3 TexCoords;
			
			uniform mat4 projection;
			uniform mat4 view;
			uniform mat4 model;
			
			void main()
			{
			    TexCoords = aPos;
			    vec4 pos =  projection * view * model *vec4(aPos, 1.0);
				gl_Position = pos.xyww;
			}

		)";

		return CodeStr;
	}
};

